package com.zmn.mcc.admin.controller.permit.role;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.business.interfaces.staff.StaffBService;
import com.zmn.mcc.common.constant.RoleDict;
import com.zmn.mcc.model.entity.permit.McPermit;
import com.zmn.mcc.model.entity.role.McRole;
import com.zmn.mcc.model.entity.role.McRoleQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/24 12:09
 */
@Controller
@RequestMapping("/permit/role/")
public class PermitRoleListController extends PermitRoleBaseController {

    @Resource
    private StaffBService staffBService;

    /**
     * 权限的角色列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                           @ModelAttribute McRoleQuery query,
                           Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setRoleId(Integer.parseInt(keyword));
            query.setRoleName("");
        } else {
            query.setRoleName(keyword);
        }
        if(NumberUtil.isNullOrZero(query.getPermitId())) {
            query.setPermitId(-1);
        }

        List<McRole> items = roleService.listPageByQuery(query);

        McPermit obj = !NumberUtil.isNullOrZero(query.getPermitId()) ? permitService.findByKey(query.getPermitId()) : new McPermit();

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("obj", obj);

        model.addAttribute("keyword", keyword);
        model.addAttribute("types", RoleDict.roleTypeMap());

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "permit/role/list";
    }
}
